<%-- 
    Document   : manager
    Created on : Nov 19, 2012, 3:20:15 PM
    Author     : BlueS
--%>

<%@page import="java.util.Date"%>
<%@page import="edu.fpt.common.Budget"%>
<%@page import="java.util.List"%>
<%@page import="edu.fpt.dal.BudgetDAL"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="pf"
          uri="/WEB-INF/tlds/pf_tag_lib.tld"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Budget Manager</title>
        <link rel="stylesheet" type="text/css" href="../Theme/style.css">
        <script src="../Theme/jquery-1.7.2.js" type="text/javascript">
        </script>
        <script src="../Theme/Script.js" type="text/javascript">
        </script>
    </head>
    <body>
        <div class="logo_holder" ><pf:logo_tag/></div>
        <%
            boolean ed = false;
            Budget edbud = null;
            boolean isTime = false;
            if(request.getParameter("year") != null && request.getParameter("month") != null) {
                isTime = true;
            }
            
            if ("POST".equals(request.getMethod())) {
                if (request.getParameter("new_name") != null && request.getParameter("new_type") != null && request.getParameter("new_amount") != null && request.getParameter("new_kind") != null) {
                    if (request.getParameter("new_name") != "" && request.getParameter("new_amount") != "") {
                        Budget entity = new Budget(1, request.getParameter("new_name"), request.getParameter("new_type"), request.getParameter("new_amount"), request.getParameter("new_kind"));
                        BudgetDAL.insertBudget(entity);
                    }
                }
                if (request.getParameter("editid")
                        != null && request.getParameter("new_name_edit") != null && request.getParameter("new_type_edit") != null && request.getParameter("new_amount_edit") != null && request.getParameter("new_kind_edit") != null) {

                    Budget entity = new Budget(Integer.parseInt(request.getParameter("editid")), request.getParameter("new_name_edit"), request.getParameter("new_type_edit"), request.getParameter("new_amount_edit"), request.getParameter("new_kind_edit"));
                    if (BudgetDAL.searchBudget(request.getParameter("editid")) != null) {
                        BudgetDAL.updateBudget(entity);
                    }
                }
            }
            if (request.getParameter("del") != null) {
                BudgetDAL.deleteBudget(request.getParameter("del"));
            }
            if (request.getParameter("edit") != null) {
                if (BudgetDAL.searchBudget(request.getParameter("edit")) != null) {
                    ed = true;
                    edbud = BudgetDAL.searchBudget(request.getParameter("edit"));
                }

            }
        %>
    <center>
        <div class="container">

            <div class="container_left"><%@include file="/leftpannel.jsp" %></div>
            <div class="container_middle">
                <div class="main_income">
                    <table>
                        <%  List list = BudgetDAL.listAll("Income");
                        if (isTime == true) {
                            list = BudgetDAL.listByTime(request.getParameter("year"),request.getParameter("month"),"Income");
                        }
                            if (list != null) {
                                for (int i = 0; i < list.size(); i++) {
                                    Budget bud = (Budget) list.get(i);
                                    if ((i % 2) == 0) {
                        %>
                        <tr>
                        <div class="entry">
                            <span class="bud-name"><%=bud.getName()%></span>
                            <span class="bud-amount"><%=bud.getAmount()%></span>
                            <span class="bud-type" ><%=bud.getType()%></span>
                            <span class="bud-time"><%=bud.getCreatedTime()%></span>
                            <span class="bud-butt"><form action="manager.jsp?edit=<%=bud.getId()%>" method="POST"><input type="submit" name="button-edit" class="butt_edit" value="EDIT"></form></span>
                            <span class="bud-del"><a href="manager.jsp?del=<%=bud.getId()%>"><img height="10px" width="10px" src="../Image/x.png"></a></span>
                        </div>
                        </tr>
                        <%
                        } else {
                        %>
                        <tr>
                        <div class="entry">
                            <span class="bud-name"><%=bud.getName()%></span>
                            <span class="bud-amount"><%=bud.getAmount()%></span>
                            <span class="bud-type" ><%=bud.getType()%></span>
                            <span class="bud-time"><%=bud.getCreatedTime()%></span>
                            <span class="bud-butt"><form action="manager.jsp?edit=<%=bud.getId()%>" method="POST"><input type="submit" name="button-edit" class="butt_edit" value="EDIT"></form></span>
                            <span class="bud-del"><a href="manager.jsp?del=<%=bud.getId()%>"><img height="10px" width="10px" src="../Image/x.png"></a></span>
                        </div>
                        </tr>
                        <%
                                    }
                                }
                            }%>
                    </table>
                </div>   

                <div class="center">
                    <% if (!ed) {%>
                    <center><div class="add"><table><tr><td><div class="add-butt">+</div></td><td>ADD NEW OUT/INCOME</td></tr></table></div></center>
                    <center>
                        <div class="add2">
                            <form action="manager.jsp" method="POST">
                                <table>
                                    <tr>
                                        <td><span class="lab">Name </span><input type="text" style="width: 200px" name="new_name"></td>
                                        <td><span class="lab"> Type </span><select name="new_type">
                                                <option>Weekly</option>
                                                <option>Monthly</option>
                                                <option>Unexpected</option>
                                            </select></td>
                                        <td><span class="lab"> Amount: </span><input type="text" style="width: 60px" name="new_amount"></td>
                                        <td><span class="lab"> Kind </span><select name="new_kind">
                                                <option>Income</option>
                                                <option>Outcome</option>
                                            </select></td>
                                        <td><input type="submit" name="button-add" class="butt_edit" value="ADD"></td>
                                        <td class="bud-del" id="add_cancel"><img height="10px" width="10px" src="../Image/x.png"></td>
                                    </tr>
                                </table>
                            </form>
                        </div>
                    </center>
                    <% } else {%>

                    <center>
                        <div class="add2_edit">
                            <form action="manager.jsp?editid=<%=edbud.getId()%>" method="POST">
                                <table>
                                    <tr>
                                        <td><span class="lab">Name </span><input type="text" style="width: 200px" name="new_name_edit" value="<%=edbud.getName()%>"></td>
                                        <td><span class="lab"> Type </span><select name="new_type_edit">
                                                <option>Weekly</option>
                                                <option>Monthly</option>
                                                <option>Unexpected</option>
                                            </select></td>
                                        <td><span class="lab">  Amount: </span><input type="text" style="width: 60px" name="new_amount_edit" value="<%=edbud.getAmount()%>"></td>
                                        <td><span class="lab"> Kind </span><select name="new_kind_edit">
                                                <option>Income</option>
                                                <option>Outcome</option>
                                            </select></td>
                                        <td><input type="submit" name="button-add" class="butt_edit" value="EDIT"></td>
                                        <td class="bud-del2" id="add_cancel"><a href="manager.jsp"><img height="10px" width="10px" src="../Image/x.png"></a></td>
                                    </tr>
                                </table>
                            </form>
                        </div>
                    </center>

                    <% }%>
                </div>

                <div class="main_outcome">

                    <table>
                        <%  List list2 = BudgetDAL.listAll("Outcome");
                        if (isTime == true) {
                            list2 = BudgetDAL.listByTime(request.getParameter("year"),request.getParameter("month"),"Outcome");
                        }
                            if (list2 != null) {
                                for (int i = 0; i < list2.size(); i++) {
                                    Budget bud = (Budget) list2.get(i);
                                    if ((i % 2) == 0) {
                        %>
                        <tr>
                        <div class="entry">
                            <span class="bud-name"><%=bud.getName()%></span>
                            <span class="bud-amount"><%=bud.getAmount()%></span>
                            <span class="bud-type" ><%=bud.getType()%></span>
                            <span class="bud-time"><%=bud.getCreatedTime()%></span>
                            <span class="bud-butt"><form action="manager.jsp?edit=<%=bud.getId()%>" method="POST"><input type="submit" name="button-edit" class="butt_edit" value="EDIT"></form></span>
                            <span class="bud-del"><a href="manager.jsp?del=<%=bud.getId()%>"><img height="10px" width="10px" src="../Image/x.png"></a></span>
                        </div>
                        </tr>
                        <%
                                    }
                                }
                            }%>
                    </table>
                </div> 
            </div>
            <div class="container_right"><%@include file="/rightpannel.jsp" %></div>

        </div>
    </center>
</body>
</html>
